package com.brsya.movie.presenter;

import com.brsya.base.base.BasePresenter;
import com.brsya.base.bean.BaseListBean;
import com.brsya.base.bean.BaseObjectBean;
import com.brsya.base.bean.MovieListBean;
import com.brsya.base.net.MyObserver;
import com.brsya.base.net.RxScheduler;
import com.brsya.movie.contract.MovieListContract;
import com.brsya.movie.model.MovieListModel;

import java.util.Map;

/**
 * Created by Brsya
 * CreateDate: 2022/7/5  20:17
 * Description:
 * 影视列表逻辑层
 */
public class MovieListPresenter extends BasePresenter<MovieListContract.View> implements MovieListContract.Presenter {
    private MovieListContract.Model model;

    public MovieListPresenter() {
        model = new MovieListModel();
    }

    @Override
    public void getMovieList(String code) {
        mView.showLoading();

        Map<String, Object> par = getPublicPar();
        if (code != null) {
            par.put("code", code);
        }

        model.getMovieList(par)
                .compose(RxScheduler.Obs_io_main())
                .to(mView.bindAutoDispose())
                .subscribe(new MyObserver<BaseObjectBean<MovieListBean>>(mView) {
                    @Override
                    public void success(BaseObjectBean<MovieListBean> baseBean) {
                        baseBean.handleData(MovieListBean.class);
                        if (baseBean.getData() == null) {
                            // 获取失败
                            return;
                        }
                        mView.showMovieList(baseBean.getData().getContentList());
                        mView.showBanner(baseBean.getData().getBannerList());
                        mView.showTab(baseBean.getData().getTagList());
                    }
                });
    }

    @Override
    public void getMovieList(int id, String page) {

        mView.showLoading();

        Map<String, Object> par = getPublicPar();
        par.put("hvId", id);
        par.put("page", page);

        final int finalId = id;
        model.getNewMovieList(par)
                .compose(RxScheduler.Obs_io_main())
                .to(mView.bindAutoDispose())
                .subscribe(new MyObserver<BaseListBean<MovieListBean.ContentListDTO.VideoListDTO>>(mView) {
                    @Override
                    public void success(BaseListBean<MovieListBean.ContentListDTO.VideoListDTO> baseBean) {
                        baseBean.handleData(MovieListBean.ContentListDTO.VideoListDTO.class);
                        if (baseBean.getData() == null) {
                            // 获取失败
                            return;
                        }
                        mView.showMovieListById(finalId, page, baseBean.getData());
                    }
                });


    }
}
